package mysql_sync

import (
	"encoding/json"
	"fmt"
	"gc-rds-ha/internal/common"
	"gc-rds-ha/internal/service/fetcher"
	msg "gc-rds-ha/scripts"

	log "github.com/sirupsen/logrus"
)

var (
	url = common.AgentConfig.Vip.Url
)

type VipS struct {
	Id         string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
	VipAddress string `protobuf:"bytes,2,opt,name=vipAddress,proto3" json:"vipAddress,omitempty"`
	RequestId  string `protobuf:"bytes,3,opt,name=requestId,proto3" json:"requestId,omitempty"`
}

// Vip 创建vip
func Vip(req *msg.STVipReq) (*msg.STVipRsp, error) {
	rsp := &msg.STVipRsp{}
	d, err := json.Marshal(req)
	if err != nil {
		log.WithFields(log.Fields{
			"req": req,
			"d":   d,
		}).Error("Vip.json.Marshal error: ")
		return rsp, err
	}
	f := &fetcher.FetchParams{
		Method: "post",
		Url:    fmt.Sprintf("%s%s", url, common.Vip),
		Data:   d,
	}
	b, err := f.HttpRequest()
	if err != nil {
		return rsp, err
	}
	if err = common.TTT.GetResponseForHttp(b, rsp); err != nil {
		return rsp, err
	}
	return rsp, nil
}

// Vips 查询vip列表
func Vips(req *msg.STVipsReq) (*msg.STVipsRsp, error) {
	var rsp = &msg.STVipsRsp{}
	d, err := json.Marshal(req)
	if err != nil {
		log.WithFields(log.Fields{
			"req": req,
			"d":   d,
		}).Error("Vips.json.Marshal error: ")
		return rsp, err
	}
	f := &fetcher.FetchParams{
		Method: "post",
		Url:    fmt.Sprintf("%s%s", url, common.Vips),
		Data:   d,
	}
	b, err := f.HttpRequest()
	if err != nil {
		return rsp, err
	}
	if err = common.TTT.GetResponseForHttp(b, rsp); err != nil {
		return rsp, err
	}
	return rsp, nil
}

// VipDelete 删除vip
func VipDelete() {

}

// VipBind vip绑定Ecs
func VipBind(req *msg.STVipBindReq) (*msg.STVipBindRsp, error) {
	var rsp = &msg.STVipBindRsp{}
	d, err := json.Marshal(req)
	if err != nil {
		log.WithFields(log.Fields{
			"req": req,
			"d":   d,
		}).Error("Vips.json.Marshal error: ")
		return rsp, err
	}
	f := &fetcher.FetchParams{
		Method: "post",
		Url:    fmt.Sprintf("%s%s", url, common.Vips),
		Data:   d,
	}
	b, err := f.HttpRequest()
	if err != nil {
		return rsp, err
	}
	if err = common.TTT.GetResponseForHttp(b, rsp); err != nil {
		return rsp, err
	}
	return rsp, nil
}

// VipUnbind Vip解绑Ecs
func VipUnbind(req *msg.STVipUnbindReq) (*msg.STVipUnbindRsp, error) {
	var rsp = &msg.STVipUnbindRsp{}
	d, err := json.Marshal(req)
	if err != nil {
		log.WithFields(log.Fields{
			"req": req,
			"d":   d,
		}).Error("Vips.json.Marshal error: ")
		return rsp, err
	}
	f := &fetcher.FetchParams{
		Method: "post",
		Url:    fmt.Sprintf("%s%s", url, common.Vips),
		Data:   d,
	}
	b, err := f.HttpRequest()
	if err != nil {
		return rsp, err
	}
	if err = common.TTT.GetResponseForHttp(b, rsp); err != nil {
		return rsp, err
	}
	return rsp, nil
}
